package test;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream;

public class FileBatchReader {
    private static final int BUFFER_SIZE = 1000;

    public static void main(String[] args) throws IOException {
        String first = "/Users/lxp/IdeaProjects/Study/MainTest/data/inData/user-test.json";
//        List<String> list = FileUtil.readUtf8Lines(first);
//        System.out.println(list.size());
        Path filePath = Paths.get(first); // 替换为实际文件路径
        int batchSize = 1000; // 每次读取的行数
        try (Stream<String> stream = Files.lines(filePath)) {
            final AtomicInteger[] count = {new AtomicInteger()};
            final List<String>[] buffer = new List[]{new ArrayList<>()};
            stream.forEach(line -> {
                buffer[0].add(line);
                count[0].getAndIncrement();
                if (count[0].get() == BUFFER_SIZE) {
                    // 处理缓冲区中的数据
                    processBuffer(buffer[0]);
                    buffer[0] = new ArrayList<>();
                    //count = 0;
                }
            });



        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private static void processBuffer(List<String> buffer) {
        // 处理缓冲区中的数据，例如计算行数、查找特定行等操作
        System.out.println("处理缓冲区数据：" + buffer.size());
    }
}
